﻿#Requires AutoHotkey v2.0

#HotIf WinExist("ahk_exe 旺店通ERP.exe")
; #HotIf WinActivate("ahk_exe 旺店通ERP.exe")
{
    ; MsgBox("请先打开旺店通")
    ; return

    ; itemHeight := 25 

    
    LocalAppData := EnvGet("LocalAppData")
    iniFile :=LocalAppData . '\ruige\set.ini'
    ; MsgBox iniFile

    ; MsgBox A_LoopFileTimeCreated

    toolConfig := {}
    
    arr := StrSplit(IniRead(iniFile, "autoTool","search"),',')
    toolConfig.search_x := arr[1]
    toolConfig.search_y := arr[2]

    arr := StrSplit(IniRead(iniFile, "autoTool","prev"),',')
    toolConfig.prev_x := arr[1]
    toolConfig.prev_y := arr[2]

    arr := StrSplit(IniRead(iniFile, "autoTool","next"),',')
    toolConfig.next_x := arr[1]
    toolConfig.next_y := arr[2]

    arr := StrSplit(IniRead(iniFile, "autoTool","selectAll"),',')
    toolConfig.selectAll_x := arr[1]
    toolConfig.selectAll_y := arr[2]

    arr := StrSplit(IniRead(iniFile, "autoTool","fenye"),',')
    toolConfig.fenye_x := arr[1]
    toolConfig.fenye_y := arr[2]

    arr := StrSplit(IniRead(iniFile, "autoTool","fy2000"),',')
    toolConfig.fy2000_x := arr[1]
    toolConfig.fy2000_y := arr[2]

    arr := StrSplit(IniRead(iniFile, "autoTool","updateGoodsPic"),',')
    toolConfig.updateGoodsPic_x := arr[1]
    toolConfig.updateGoodsPic_y := arr[2]

    
    arr := StrSplit(IniRead(iniFile, "autoTool","updateGoodsInfo"),',')
    toolConfig.updateGoodsInfo_x := arr[1]
    toolConfig.updateGoodsInfo_y := arr[2]

    arr := StrSplit(IniRead(iniFile, "autoTool","dianpu"),',')
    toolConfig.dianpu_x := arr[1]
    toolConfig.dianpu_y := arr[2]

    arr := StrSplit(IniRead(iniFile, "autoTool","dianpuBox"),',')
    toolConfig.dianpuBox_x := arr[1]
    toolConfig.dianpuBox_y := arr[2]

    ; arr := StrSplit(IniRead(iniFile, "autoTool","dianpuAll"),',')
    ; toolConfig.dianpuAll_x := arr[1]
    ; toolConfig.dianpuAll_y := arr[2]

    arr := StrSplit(IniRead(iniFile, "autoTool","rightClick"),',')
    toolConfig.rightClick_x := arr[1]
    toolConfig.rightClick_y := arr[2]


    showDialog(toolConfig)

}



showDialog(toolConfig) 
{
    ;店铺数量
    sc := IniRead("toolConfig.ini", "tool", "count" ,"10")

    ; 创建一个窗口
    MyGui := Gui("+Resize -MaximizeBox","辅助工具")
    ; Gui +Resize
    MyGui.Add( "Checkbox", "x30 y20 w120 h30 vCheck1" , "检查店铺是否可选")
    MyGui.Add("Text","x30 y60 " ,"店铺数量")
    MyGui.Add("Edit", "x90 y56 w60 h20 vShopCount", sc)
    MyGui.Add("Button", "x30 y120 w100 h30" , "更新图片").OnEvent("Click", buttonOk)
    MyGui.Show("xCenter yCenter w200 h200")


    buttonOk(thisgui,*)
    {
        Saved := MyGui.Submit()  ; 将命名控件的内容保存到一个对象中.

        if(IsNumber(Saved.ShopCount) == false) {
            MsgBox("输入的内容不是数字")
            return
        }

        ; MsgBox("You entered '" Saved.Check1 " " Saved.ShopCount "'.")
        
        IniWrite(Saved.ShopCount,"toolConfig.ini", "tool", "count" )

        doUpdatePic(Saved.ShopCount, toolConfig)

    }

}


doUpdatePic(count , toolConfig)
{


    WinActivate("ahk_exe 旺店通ERP.exe")
    WinMaximize("ahk_exe 旺店通ERP.exe")

    TrayTip("正在自动处理 !!! ","辅助工具")

    ;分页
    Click("Right", toolConfig.selectAll_x , toolConfig.selectAll_y)  ; 表格左上角全选单元格
    Sleep(500)
    Click("Left", toolConfig.fenye_x, toolConfig.fenye_y)
    Sleep(100)
    Click("Left",toolConfig.fy2000_x, toolConfig.fy2000_y)
    Sleep(400)

    ; ; 店铺改成全部
    ; Click("Left", toolConfig.dianpuBox_x, toolConfig.dianpuBox_y)
    ; Sleep(200)
    ; Click("Left", toolConfig.dianpuAll_x, toolConfig.dianpuAll_y )
    ; Sleep(200)

    ;焦点放到店铺上, 并使用方向键选择全部
    Click(toolConfig.dianpu_x, toolConfig.dianpu_y )
    Sleep(1200)
    SendInput("{TAB}")
    Sleep(1100)
    loop 100 {
        SendInput("{Up}")
        Sleep(100)
    }

    
    
    nextPage := false 
    nextShop := true 
    pageTotal :=1
    currentIndex := 1
    shopCount :=0

    c:= 1
    loop 99999 {
        if (shopCount> count) {
            break
        }
        
        ;焦点放到店铺上, 并使用方向键选择下一个店铺
        if (nextShop) {
            shopCount++

            Click(toolConfig.dianpu_x, toolConfig.dianpu_y )
            Sleep(500)
            SendInput("{TAB}")
            Sleep(500)
            SendInput("{Down}")
            Sleep(1000)
        
            ; 搜索
            Click(toolConfig.search_x, toolConfig.search_y)
            ; TrayTip("正在搜索", "1aaaa")
            Sleep(1000)
            
            currentIndex :=1
            pageTotal :=1
        }
        else if (nextPage) {
            Click(toolConfig.next_x, toolConfig.next_y )   
            Sleep(1000)
        }



        ;全选
        Click("Left", toolConfig.selectAll_x, toolConfig.selectAll_y)   
        Sleep(1600)

        MouseClick("Right", toolConfig.rightClick_x, toolConfig.rightClick_y)   ;右键
        ; MouseClick("Right", toolConfig.selectAll_x+20, toolConfig.selectAll_y+20)   ;右键
        Sleep(3550)

        Click(toolConfig.updateGoodsPic_x, toolConfig.updateGoodsPic_y )   ;更新图片按钮
        Sleep(1000)
        SendInput("!y")

        Sleep(1300)
        SendInput("{enter}")

        Sleep(110)

        ; 换页
        if (pageTotal <0 || pageTotal <= currentIndex ) {
            cmdStr :=  RunWaitOne("AutoToolApp.exe")
            pageInfo := StrSplit(cmdStr, ',')
            if (pageInfo.Length>1 && pageInfo[1] !=0 && pageInfo[2] != 0) {
                pageTotal:=pageInfo[1]
                if (pageInfo[1] == pageInfo[2]) {
                    ; 完成一个店铺
                    nextShop:=true 
                    nextPage := false
                }
                else {
                    ;下一页
                    nextShop := false
                    nextPage := true
                }
            }
            else{
                ; 错误
                MsgBox  ("错误 : " . cmdStr)
                return
            }
        }
       
        currentIndex++

        c++
    }

    TrayTip 
    ToolTip()
    MsgBox("更新完成 !!! ")
}


RunWaitOne(command) {
    ; WshShell object: http://msdn.microsoft.com/en-us/library/aew9yb99
    shell := ComObject("WScript.Shell")
    ; 通过 cmd.exe 执行单条命令
    exec := shell.Exec(A_ComSpec " /C " command)
    ; 读取并返回命令的输出
    return exec.StdOut.ReadAll()
}